package com.zbkj.crmeb.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CommonPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.crmeb.chant.model.ChannelMerchant;
import com.zbkj.crmeb.chant.service.IChannelMerchantService;
import com.zbkj.crmeb.enums.BrandAuditEnum;
import com.zbkj.crmeb.store.dao.StoreBrandAuditDao;
import com.zbkj.crmeb.store.model.StoreBrandAudit;
import com.zbkj.crmeb.store.request.StoreBrandAuditRequest;
import com.zbkj.crmeb.store.response.StoreBrandAuditResponse;
import com.zbkj.crmeb.store.service.StoreBrandAuditService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class StoreBrandAuditServiceImpl extends ServiceImpl<StoreBrandAuditDao, StoreBrandAudit> implements StoreBrandAuditService {
    @Autowired
    private IChannelMerchantService channelMerchantService;

    /**
     * 功能描述: 总后台获取品牌审核列表
     * @Param: [request]
     * @Return: com.common.CommonPage<com.zbkj.crmeb.store.response.StoreBrandAuditResponse>
     * @Author: wangzh
     * @Date: 2023/6/1 13:58
     */
    @Override
    public CommonPage<StoreBrandAuditResponse> getList(StoreBrandAuditRequest request) {
        LambdaQueryWrapper<StoreBrandAudit> queryWrapper = new LambdaQueryWrapper<>();
        // 品牌名称检索
        if (StringUtils.isNotBlank(request.getEnName())) {
            queryWrapper.like(StoreBrandAudit::getEnName, request.getEnName());
        }
        // 渠道检索
        if (Objects.nonNull(request.getIdentityType())) {
            queryWrapper.eq(StoreBrandAudit::getIdentityType, request.getIdentityType());
        }
        // 状态检索
        if (Objects.nonNull(request.getStatus())) {
            queryWrapper.eq(StoreBrandAudit::getStatus, request.getStatus());
        }
        Page<Object> page = PageHelper.startPage(request.getPage(), request.getLimit());
        List<StoreBrandAuditResponse> responses = new ArrayList<>();
        List<StoreBrandAudit> storeBrandAudits = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(storeBrandAudits)) {
            return CommonPage.restPage(CommonPage.copyPageInfo(page, responses));
        }
        List<Integer> merIds = storeBrandAudits.stream().map(StoreBrandAudit::getMerId).distinct().collect(Collectors.toList());
        List<ChannelMerchant> channelMerchants = channelMerchantService.listByIds(merIds);
        Map<Integer, ChannelMerchant> channelMerchantMap = channelMerchants.stream().collect(Collectors.toMap(ChannelMerchant::getId, e -> e, (v1, v2) -> v1));
        for (StoreBrandAudit storeBrandAudit : storeBrandAudits) {
            StoreBrandAuditResponse response = new StoreBrandAuditResponse();
            BeanUtils.copyProperties(storeBrandAudit, response);
            response.setStatus(BrandAuditEnum.getEnumByCode(storeBrandAudit.getStatus()).getMsg());
            response.setIdentityType("渠道商户");
            if (storeBrandAudit.getIdentityType() == 2) {
                response.setIdentityType("供应商");
            }
            response.setMerName(channelMerchantMap.get(storeBrandAudit.getMerId()).getChannelName());
            responses.add(response);
        }
        PageInfo<StoreBrandAuditResponse> storeBrandAuditResponsePageInfo = CommonPage.copyPageInfo(page, responses);
        return CommonPage.restPage(storeBrandAuditResponsePageInfo);
    }

    /**
     * 功能描述: 驳回品牌
     * @Param: [id]
     * @Return: java.lang.Boolean
     * @Author: wangzh
     * @Date: 2023/6/1 13:58
     */
    @Override
    public Boolean rejectBrand(Integer id) {
        return this.update(Wrappers.<StoreBrandAudit>lambdaUpdate().eq(StoreBrandAudit::getId, id).set(StoreBrandAudit::getStatus, BrandAuditEnum.HAVE_BEEN_REJECTED.getCode()));
    }

    /**
     * 功能描述: 申请品牌
     * @Param: [request, merId]
     * @Return: java.lang.Boolean
     * @Author: wangzh
     * @Date: 2023/6/1 13:57
     */
    @Override
    public Boolean applyBrand(StoreBrandAuditRequest request, Integer merId) {
        ChannelMerchant channelMerchant = channelMerchantService.getChannelMerchantByMerId(merId);
        StoreBrandAudit storeBrandAudit = new StoreBrandAudit();
        BeanUtils.copyProperties(request, storeBrandAudit);
        storeBrandAudit.setMerId(merId);
        storeBrandAudit.setIdentityType(channelMerchant.getChannelType() == 3 ? 2 : 1);
        return this.save(storeBrandAudit);
    }
}
